package com.mk.api.file;

import com.mk.base.model.response.Rsp;
import com.mk.model.request.PacRequest;
import com.mk.service.IFileInfoService;
import com.mk.service.IPacService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: yq
 * @Description:
 * @Date: Created in 11:01 AM 3/17/18
 */
@RestController
@RequestMapping("/pac")
public class PacApi {

    @Autowired
    private IPacService pacService;
    @Autowired
    private IFileInfoService fileInfoService;

    @RequestMapping("/open")
    public Rsp openPac(@RequestParam("id")Long id){
        return Rsp.SUCCESS(pacService.get(id));
    }

    @RequestMapping("/load")
    public Rsp loadPaces(@RequestParam("dirId")Long dirId){
        return Rsp.SUCCESS(pacService.getByDirId(dirId));
    }

    @RequestMapping(method = RequestMethod.POST)
    public Rsp savePac(@ModelAttribute PacRequest request){
        try{
            return Rsp.SUCCESS(pacService.savePac(request));
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Rsp updatePac(@ModelAttribute PacRequest request){
        try{
            if(pacService.updatePac(request))
                return Rsp.SUCCESS("编辑资料包信息成功");
            else
                return Rsp.ERROR("编辑资料包信息失败");
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public Rsp deletePac(@PathVariable("id")Long id){
        try{
            if(pacService.deletePac(id))
                return Rsp.SUCCESS("删除资料包信息成功");
            else
                return Rsp.ERROR("删除资料包信息失败");
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    @RequestMapping(value = "/move",method = RequestMethod.POST)
    public Rsp movePac(@ModelAttribute("pac") PacRequest request) {
        try{
            if(pacService.movePac(request))
                return Rsp.SUCCESS("移动资料包信息成功");
            else
                return Rsp.ERROR("移动资料包信息失败");
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    /**
     * 加载资料包下所有文件
     * @param dirId
     * @param pacId
     * @return
     */
    @RequestMapping(value = "/files",method = RequestMethod.GET)
    public Rsp loadAllFiles(@RequestParam("dirId") Long dirId,
                            @RequestParam("pacId") Long pacId){
        try{
            return Rsp.SUCCESS(fileInfoService.findByDirId(dirId,pacId));
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }

    /**
     * 删除单个文件
     * @param id
     * @return
     */
    @RequestMapping(value = "/del/file",method = RequestMethod.DELETE)
    public Rsp deleteFile(Long id){
        try{
            if(fileInfoService.deleteFile(id))
                return Rsp.SUCCESS("删除文件信息成功");
            else
                return Rsp.ERROR("删除文件信息失败");
        }catch (Exception e){
            return Rsp.ERROR(e.getMessage());
        }
    }
}
